/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package es.ihatetothink.restaurantesCeliacos;

import java.io.IOException;
import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

	private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
	private Context c;
	
	public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		c = mapView.getContext();
	}
	


	public void addOverlay(OverlayItem overlay) {
	    m_overlays.add(overlay);
	  //  populate();
	}
	public void myPopulate(){
		populate();
	}
	@Override
	protected OverlayItem createItem(int i) {
		return m_overlays.get(i);
	}

	@Override
	public int size() {
		return m_overlays.size();
		
	}

		
		
	
	@Override
	protected boolean onBalloonTap(final int index, OverlayItem item) throws IOException {
		final restaurante r1 = new restaurante(index+1,c);
		CharSequence[] items = {"Llamar a: "+r1.getTelefono(), "Visitar web: "+r1.getWeb().replace("http://", "").replace("www.", "")};
		if(r1.getTelefono()==null){
			items[0]="No hay telefono";
			
		}
		if(r1.getWeb()==null){
			items[1]="No hay Web";
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(c);
		builder.setTitle("Seleccione la accion a realizar");
		
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		    	switch (item) {
				case 0: //llamar
					if (r1.getTelefono() != null) {
						Intent callIntent = new Intent(Intent.ACTION_CALL);
				        callIntent.setData(Uri.parse("tel:"+r1.getTelefono()));
				        
				        c.startActivity(callIntent);
				        
					}
					break;
				case 1: //web
					if (r1.getWeb() != null) {
						try{
						Intent webIntent = new Intent(Intent.ACTION_VIEW);
						webIntent.setData(Uri.parse(r1.getWeb())); 
						c.startActivity(webIntent);
						}
						catch (Exception e) {
							// TODO: handle exception
						}
					}
					break;
				default:
					break;
				}
		    }
		});
		
		AlertDialog alert = builder.create();
		alert.show();
		return true;
		
	}
	
}
